package top.chengdongqing.mishop.product.vo

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import top.chengdongqing.mishop.entities.ProductSKU
import java.io.Serializable
import java.math.BigDecimal

data class ProductSKUVO(
    val id: Int,
    val name: String,
    val price: BigDecimal,
    val originalPrice: BigDecimal?,
    val pictureUrl: String,
    val gallery: List<String>,
    val attributes: List<SKUAttributes>,
    val available: Boolean
) : Serializable {
    companion object {
        fun of(sku: ProductSKU): ProductSKUVO {
            return ProductSKUVO(
                sku.id,
                sku.name,
                sku.price,
                sku.originalPrice,
                sku.pictureUrl,
                jacksonObjectMapper().readValue(sku.gallery),
                jacksonObjectMapper().readValue(sku.attributes),
                sku.stocks > 0
            )
        }
    }
}

data class SKUAttributes(
    val name: String,
    val value: String
) : Serializable